<?php
/**
 +------------------------------------------------------------------------------
 * SoloPHP PHP框架
 +------------------------------------------------------------------------------
 * $Author: Yotianya <yotianya@gmail.com>
 * $Dtime:2011-10-24
 * $Copyright: CopyRight By SoloPHP Yotianya
 +------------------------------------------------------------------------------
 * Sae 数据库的驱动支持
 +------------------------------------------------------------------------------
 */
class Sae {

	public $conn;//数据库链接句柄
	public $arrSql;//执行的SQL语句记录

/*
 +------------------------------------------------------------------------------
* 构造函数
 +------------------------------------------------------------------------------
 */
	public function __construct($dbConfig)
	{
		sae_set_display_errors(TRUE);
		$this->conn = @new SaeMysql();
		if( $this->conn->errno() )errorMsg('dbConnectFail',$this->conn->error()); 
		$this->conn->setCharset("UTF8");
	}
/*
 +------------------------------------------------------------------------------
 * 按SQL语句获取记录结果，返回数组
 +------------------------------------------------------------------------------
 */
	public function getArray($sql)
	{
		$this->arrSql[] = $sql;
		$result = $this->conn->getData($sql);
		if( $this->conn->errno() )errorMsg('queryError',"{$sql}<br />执行错误: " . $this->conn->error());
		return $result;
	}
/*
 +------------------------------------------------------------------------------
 * 返回当前插入记录的主键ID
 +------------------------------------------------------------------------------
 */	
	public function newinsertid()
	{
		return $this->conn->lastId();
	}
/*
 +------------------------------------------------------------------------------
 * 格式化带limit的SQL语句
 +------------------------------------------------------------------------------
 */	
	public function setlimit($sql, $limit)
	{
		return $sql. " LIMIT {$limit}";
	}
/*
 +------------------------------------------------------------------------------
 *执行一个SQL语句
 +------------------------------------------------------------------------------
 */
	public function exec($sql)
	{
		$this->arrSql[] = $sql;
		$result = $this->conn->runSql($sql);
		if( $this->conn->errno() )errorMsg('queryError',"{$sql}<br />执行错误: " . $this->conn->error());
		return $result;
	}
/*
 +------------------------------------------------------------------------------
 * 返回影响行数
 +------------------------------------------------------------------------------
 */	
	public function affected_rows()
	{
		return FALSE; // SAE环境暂时无法获取影响行数
	}
/*
 +------------------------------------------------------------------------------
 *获取数据表结构
 +------------------------------------------------------------------------------
 */
	public function getTable($tbl_name)
	{
		return $this->getArray("DESCRIBE {$tbl_name}");
	}

/*
 +------------------------------------------------------------------------------
 * 对特殊字符进行过滤
 *
 * @param value  值
 +------------------------------------------------------------------------------
 */
	public function escape($value, $quotes = FALSE) {
		if(is_null($value))return 'NULL';
		if(is_bool($value))return $value ? 1 : 0;
		if(is_int($value))return (int)$value;
		if(is_float($value))return (float)$value;
		if(@get_magic_quotes_gpc())$value = stripslashes($value);
		return '\''.$this->conn->escape($value).'\'';
	}
/*
 +------------------------------------------------------------------------------
 * 析构函数
 +------------------------------------------------------------------------------
 */
	public function __destruct()
	{
		@$this->conn->closeDb();
	}
/*
 +------------------------------------------------------------------------------
 * getConn 取得Sae MySQL对象
 * 为了更好地使用Sea提供MySQL类，getSeaDB函数将返回Sae MySQL对象供开发者使用
 +------------------------------------------------------------------------------
 */	
	public function getConn()
	{
		return $this->conn;
	}
}

